import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path:'/welcome',
        name:'welcome',
        component:() => import('../views/WelcomeView.vue')
      },
      {
        path:'/users',
        name:'users',
        component:() => import('../views/UsersView.vue')
      },
      {
        path:'/goods',
        name:'goods',
        component:() => import('../views/GoodsView.vue')
      },
      {
        path:'/rights',
        name:'rights',
        component:() => import('../views/RightsView.vue')
      },
      {
        path:'/roles',
        name:'roles',
        component:() => import('../views/RolesView.vue')
      }
    ],
    redirect:'/welcome'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,form,next)=>{
  if(to.path==='/login'){
    next()
  }else{
    // 获取本地存储
    let token = sessionStorage.getItem('token')

    // 如果没有token
    if(!token){
      // 拦截路由去/login
      next('/login')
    }else{
      next()
    }
  }
})

export default router
